package com.example.petshopping.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.petshopping.R;
import com.example.petshopping.ui.login.LoginActivity;

/**
 * 启动页面 - 显示轮播图片
 */
public class SplashActivity extends AppCompatActivity {
    
    private ImageView ivSplash;
    private Handler handler;
    private Runnable imageSwapRunnable;
    private int currentStep = 0;
    
    // 图片资源数组
    private final int[] splashImages = {
        R.drawable.start1,
        R.drawable.start2
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        initViews();
        startImageRotation();
    }
    
    private void initViews() {
        ivSplash = findViewById(R.id.iv_splash);
        handler = new Handler();
        
        // 设置初始图片
        ivSplash.setImageResource(splashImages[0]);
    }
    

    
    private void startImageRotation() {
        imageSwapRunnable = new Runnable() {
            @Override
            public void run() {
                currentStep++;
                
                if (currentStep == 1) {
                    // 3秒后切换到第二张图片
                    ivSplash.setImageResource(splashImages[1]);
                    // 再等3秒后跳转到登录页
                    handler.postDelayed(this, 3000);
                } else {
                    // 6秒后跳转到登录页面
                    navigateToLogin();
                }
            }
        };
        
        // 3秒后开始第一次切换
        handler.postDelayed(imageSwapRunnable, 3000);
    }
    
    private void navigateToLogin() {
        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
        startActivity(intent);
        finish(); // 结束当前活动，防止用户返回到启动页
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 清理Handler，防止内存泄漏
        if (handler != null && imageSwapRunnable != null) {
            handler.removeCallbacks(imageSwapRunnable);
        }
    }
    
    @Override
    public void onBackPressed() {
        // 禁用返回键，防止用户在启动页按返回键
        // 可以选择不调用super.onBackPressed()来完全禁用，或者调用来允许退出应用
        super.onBackPressed();
    }
} 